package com.whyalwaysmea.comment.controller.content;

import com.whyalwaysmea.comment.constant.DicTypeConst;
import com.whyalwaysmea.comment.dto.BusinessDto;
import com.whyalwaysmea.comment.service.BusinessService;
import com.whyalwaysmea.comment.service.DicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 * @Author: Long
 * @Date: Create in 10:05 2017/12/12
 * @Description:
 */

@Controller
@RequestMapping("/businesses")
public class BusinessesController {

    @Autowired
    private DicService dicService;

    @Autowired
    private BusinessService businessService;

    /**
     * 商户列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public String search(Model model, BusinessDto dto) {
        model.addAttribute("list", businessService.searchByPage(dto));
        model.addAttribute("searchParam", dto);
        return "/content/businessList";
    }

    /**
     * 删除商户
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String remove(@PathVariable("id") Long id) {
        System.out.println("id == " + id);
        return "redirect:/businesses";
    }

    /**
     * 商户新增页初始化
     */
    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public String addInit(Model model) {
        model.addAttribute("cityList", dicService.getListByType(DicTypeConst.CITY));
        model.addAttribute("categoryList", dicService.getListByType(DicTypeConst.CATEGORY));
        return "/content/businessAdd";
    }

    /**
     * 商户修改页初始化
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String modifyInit(Model model, @PathVariable("id") Long id) {
        model.addAttribute("cityList", dicService.getListByType(DicTypeConst.CITY));
        model.addAttribute("categoryList", dicService.getListByType(DicTypeConst.CATEGORY));
        model.addAttribute("modifyObj", businessService.getById(id));
        return "/content/businessModify";
    }

    /**
     * 商户修改
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String modify(@PathVariable("id") Long id, BusinessDto dto) {
        System.out.println(id);
        System.out.println("dto = " + dto.getDesc());
        return "/content/businessModify";
    }
}
